package com.hytc.controller;

import com.hytc.entity.Cart;
import com.hytc.entity.Product;
import com.hytc.entity.User;
import com.hytc.service.UserService;
import com.hytc.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class DeleteServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 在购物车中删除商品
         */
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");
        int commodityId = Integer.parseInt(req.getParameter("commodity_id"));
        int user_id = Integer.parseInt(req.getParameter("user_id"));
        if (user != null) {

            userService.delete(commodityId, user_id);
            List<Cart> cart1 = userService.cart(user.getId());
            double sum1 = 0;
            double sum2 = 0;
            for (Cart cart : cart1) {
                sum1 += cart.getBuy_price();
            }
            for (Cart cart : cart1) {
                sum2 += cart.getPrice();
            }
            session.setAttribute("sum1", sum1 - sum2);
            session.setAttribute("sum2", sum2);
            session.setAttribute("cart", cart1);
//            resp.sendRedirect("/buy.jsp");
            req.getRequestDispatcher("/buy.jsp").forward(req, resp);

        }
    }
}
